import { reqGetGoodsInfo, reqAddorUpdataShopCart } from '@/api/index.js'
import { getUUID } from '@/utils/uuid_token.js'
// search模块的小仓库
const state = {
  goodsInfo: {},
  // 游客的临时id
  uuid_token: getUUID()
};

const mutations = {
  GETGOODSINFO(state, goodsInfo) {
    state.goodsInfo = goodsInfo
  }
};

const actions = {
  // 获取search模块数据
  async getGoodsInfo({ commit }, skuid) {
    // 当前reqGetSearchInfo()在调用时至少传入一个参数(空对象)
    //skuid参数是当用户派发action的时候，作为第二个参数传递过来的，至少是一个空对象
    let result = await reqGetGoodsInfo(skuid);
    if (result.code === 200) {
      commit("GETGOODSINFO", result.data)
    }
  },

  async addorUpdataShopCart({ commit }, { skuid, skuNum }) {
    let result = await reqAddorUpdataShopCart(skuid, skuNum);
    if (result.code == 200) {
      return 'ok'
    } else {
      return Promise.reject(new Error('faile'))
    }
  },
};

// 计算属性
// 项目中getters的作用是，简化仓库中的数据
const getters = {
  categoryView(state) {
    return state.goodsInfo.categoryView || {}
  },
  skuInfo(state) {
    return state.goodsInfo.skuInfo || {}
  },
  spuSaleAttrList(stat) {
    return state.goodsInfo.spuSaleAttrList || []
  }
};


export default {
  state,
  mutations,
  actions,
  getters

}